<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('vl_language.class.php');


class VL_Language_DBC extends VL_Language implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);


		$ret = false;

		$sql = "select code, en_name, local_name from vl_languages where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_code(stripslashes($rez_obj->CODE));
			$this->set_en_name(stripslashes($rez_obj->EN_NAME));
			$this->set_local_name(stripslashes($rez_obj->LOCAL_NAME));

			$ret = $id;
		}


		return $ret;
	}

	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('vl_languages_seq');
		$code = addslashes($this->get_code());
		$en_name = addslashes($this->get_en_name());
		$local_name = addslashes($this->get_local_name());

		if ($id) {
			$sql = "insert into sites (id, ".
									      "code, ".
									      "en_name, ".
										  "local_name ".
									      ") ".
								"values ".
									      "($id,".
									      "'$code', ".
									      "'$en_name', ".
										  "'$local_name' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('vl_languages_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();
		$code = addslashes($this->get_code());
		$en_name = addslashes($this->get_en_name());
		$local_name = addslashes($this->get_local_name());


		$sql = "update sites set ".
							      "code = '$code', ".
							      "en_name = '$en_name', ".
							      "local_name = '$local_name' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public function load_by_code(DB_Connection $dbc, $code) {
		$ret = false;

		$code = addslashes($code);

		$sql = "select id, en_name, local_name from vl_languages where code = '$code'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($rez_obj->ID);
			$this->set_code($code);
			$this->set_en_name(stripslashes($rez_obj->EN_NAME));
			$this->set_local_name(stripslashes($rez_obj->LOCAL_NAME));

			$ret = $rez_obj->ID;
		}

		return $ret;
	}


	public static function load_all_languages_as_array(DB_Connection $dbc) {
		$sql = "select id, code, en_name, local_name from vl_languages";
		$rez = $dbc->execute($sql);
		$ret = array();

		while($rez_obj = $rez->fetch_object()) {
			$ret[$rez_obj->CODE]['id'] = $rez_obj->ID;
			$ret[$rez_obj->CODE]['en_name'] = $rez_obj->EN_NAME;
			$ret[$rez_obj->CODE]['local_name'] = $rez_obj->LOCAL_NAME;
		}

		return $ret;
	}


	public static function load_all_languages_ids(DB_Connection $dbc) {
		$sql = "select id, code  from vl_languages";
		$rez = $dbc->execute($sql);
		$ret = array();
		while($rez_obj = $rez->fetch_object()) {
			$ret[$rez_obj->CODE] = $rez_obj->ID;
		}

		return $ret;
	}
}